package com.home.duy.code.components;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

public class JTextFieldCurrency extends JFormattedTextField implements
		PropertyChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private NumberFormat formatFloat;
	private NumberFormat editFormatFloat;
	private float value = 0;

	public JTextFieldCurrency() {
		formatFloat = NumberFormat.getCurrencyInstance();
		formatFloat.setMinimumFractionDigits(0);
		editFormatFloat = NumberFormat.getNumberInstance();
		this.setFormatterFactory(new DefaultFormatterFactory(
				new NumberFormatter(formatFloat), new NumberFormatter(
						formatFloat), new NumberFormatter(editFormatFloat)));
		this.setColumns(20);
		this.setValue(value);
		this.setHorizontalAlignment(SwingConstants.RIGHT);
		this.addPropertyChangeListener("value", this);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		value = ((Number) this.getValue()).intValue();
	}

}
